package seatechit.msbgateway.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;

public class AppUtils extends Global {

	private static char[] SPECIAL_CHARACTERS = { ' ', '!', '"', '#', '$', '%', '*', '+', ',', ':', '<', '=', '>', '?', '@', '[', '\\', ']', '^', '`', '|', '~', 'À', 'Á', 'Â', 'Ã',
			'È', 'É', 'Ê', 'Ì', 'Í', 'Ò', 'Ó', 'Ô', 'Õ', 'Ù', 'Ú', 'Ý', 'à', 'á', 'â', 'ã', 'è', 'é', 'ê', 'ì', 'í', 'ò', 'ó', 'ô', 'õ', 'ù', 'ú', 'ý', 'Ă', 'ă', 'Đ', 'đ', 'Ĩ',
			'ĩ', 'Ũ', 'ũ', 'Ơ', 'ơ', 'Ư', 'ư', 'Ạ', 'ạ', 'Ả', 'ả', 'Ấ', 'ấ', 'Ầ', 'ầ', 'Ẩ', 'ẩ', 'Ẫ', 'ẫ', 'Ậ', 'ậ', 'Ắ', 'ắ', 'Ằ', 'ằ', 'Ẳ', 'ẳ', 'Ẵ', 'ẵ', 'Ặ', 'ặ', 'Ẹ', 'ẹ',
			'Ẻ', 'ẻ', 'Ẽ', 'ẽ', 'Ế', 'ế', 'Ề', 'ề', 'Ể', 'ể', 'Ễ', 'ễ', 'Ệ', 'ệ', 'Ỉ', 'ỉ', 'Ị', 'ị', 'Ọ', 'ọ', 'Ỏ', 'ỏ', 'Ố', 'ố', 'Ồ', 'ồ', 'Ổ', 'ổ', 'Ỗ', 'ỗ', 'Ộ', 'ộ', 'Ớ',
			'ớ', 'Ờ', 'ờ', 'Ở', 'ở', 'Ỡ', 'ỡ', 'Ợ', 'ợ', 'Ụ', 'ụ', 'Ủ', 'ủ', 'Ứ', 'ứ', 'Ừ', 'ừ', 'Ử', 'ử', 'Ữ', 'ữ', 'Ự', 'ự', };

	private static char[] REPLACEMENTS = { ' ', '\0', '\0', '\0', '\0', '\0', '\0', '_', '\0', '_', '\0', '\0', '\0', '\0', '\0', '\0', '_', '\0', '\0', '\0', '\0', '\0', 'A',
			'A', 'A', 'A', 'E', 'E', 'E', 'I', 'I', 'O', 'O', 'O', 'O', 'U', 'U', 'Y', 'a', 'a', 'a', 'a', 'e', 'e', 'e', 'i', 'i', 'o', 'o', 'o', 'o', 'u', 'u', 'y', 'A', 'a',
			'D', 'd', 'I', 'i', 'U', 'u', 'O', 'o', 'U', 'u', 'A', 'a', 'A', 'a', 'A', 'a', 'A', 'a', 'A', 'a', 'A', 'a', 'A', 'a', 'A', 'a', 'A', 'a', 'A', 'a', 'A', 'a', 'A',
			'a', 'E', 'e', 'E', 'e', 'E', 'e', 'E', 'e', 'E', 'e', 'E', 'e', 'E', 'e', 'E', 'e', 'I', 'i', 'I', 'i', 'O', 'o', 'O', 'o', 'O', 'o', 'O', 'o', 'O', 'o', 'O', 'o',
			'O', 'o', 'O', 'o', 'O', 'o', 'O', 'o', 'O', 'o', 'O', 'o', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', };

	/**
	 * Replace Vietnames character in string by
	 * 
	 * @param s
	 * @return
	 */
	public static String convertToVietnamesNoSign(String s) {
		int maxLength = Math.min(s.length(), 236);
		char[] buffer = new char[maxLength];
		int n = 0;
		for (int i = 0; i < maxLength; i++) {
			char ch = s.charAt(i);
			int index = Arrays.binarySearch(SPECIAL_CHARACTERS, ch);
			if (index >= 0) {
				buffer[n] = REPLACEMENTS[index];
			} else {
				buffer[n] = ch;
			}
			if (buffer[n] > 31) {
				n++;
			}
		}
		while (n > 0 && buffer[n - 1] == '/') {
			n--;
		}
		return String.valueOf(buffer, 0, n);
	}

	public static String XMLReplaceSpecChar(String input) {
		if (input != null) {
			input = input.replace("\\<", "");
			input = input.replace("<", "");
			input = input.replace("\\>", "");
			input = input.replace(">", "");
			return input;
		} else {
			return "";
		}
	}

	/**
	 * 
	 * @param responseMessage
	 * @param startPos
	 * @param recordLength
	 * @return
	 */
	public static ArrayList<String[]> getListOfResponseRow(final String[] responseMessage, final int startPos, final int recordLength) {
		ArrayList<String[]> retArrString = new ArrayList<String[]>();
		int i = 0;
		while (startPos + recordLength * i < responseMessage.length) {
			int intMissingCharLength = responseMessage.length - (startPos + recordLength * (i + 1));
			String[] currentRecord = new String[recordLength];
			if (intMissingCharLength < 0) {
				System.arraycopy(responseMessage, (startPos + recordLength * i), currentRecord, 0, recordLength + intMissingCharLength);
			} else {
				System.arraycopy(responseMessage, (startPos + recordLength * i), currentRecord, 0, recordLength);

			}
			retArrString.add(currentRecord);
			i++;
		}
		return retArrString;
	}

	/**
	 * 
	 * 
	 * @param julianDate
	 * @return Date with julian format
	 * @throws ParseException
	 */
	public static Date getDateFromJulian7(String julianDate) throws ParseException {
		return new SimpleDateFormat("yyyyD").parse(julianDate);
	}

	/**
	 * Converts java.util.Date to JulianDate format(yyyyD)
	 * 
	 * @param date
	 * @return
	 */
	public static String getJulian7FromDate(Date date) {
		StringBuilder sb = new StringBuilder();
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);

		return sb.append(cal.get(Calendar.YEAR)).append(String.format("%03d", cal.get(Calendar.DAY_OF_YEAR))).toString();
	}

	/**
	 * Get current datetime in specified format.
	 * 
	 * @param dateFormat
	 * @return
	 */
	public static String now(String dateFormat) {
		Calendar cal = Calendar.getInstance();
		if (dateFormat.equals(Global.DEF_FORMAT_DATE_TIME_FULL)) {
			return String.valueOf(cal.getTimeInMillis());
		} else {
			SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
			return sdf.format(cal.getTime());
		}
	}

	/**
	 * Check input string is valid number or not
	 * 
	 * @param str
	 * @return
	 */
	public static boolean isValidNumber(String str) {
		if (str == null || str.length() == 0)
			return false;
		try {
			Double.parseDouble(str);
		} catch (NumberFormatException ex) {
			return false;
		}
		return true;
	}

	public static boolean isValidNumeric(String str) {
		String s = str;
		for (int i = 0; i < s.length(); i++) {
			if (!Character.isDigit(s.charAt(i)))
				return false;
		}
		return true;
	}

	/**
	 * Check input string is valid datetime or not
	 * 
	 * @param inputDate
	 * @return
	 */
	public static boolean isValidDate(String inputDate, String format) {
		if (inputDate == null)
			return false;
		// set the format to use as a constructor argument
		SimpleDateFormat dateFormat = new SimpleDateFormat(format);
		if (inputDate.trim().length() != dateFormat.toPattern().length())
			return false;
		dateFormat.setLenient(false);
		try {
			// parse the inDate parameter
			dateFormat.parse(inputDate.trim());
		} catch (ParseException pe) {
			return false;
		}
		return true;
	}

	// yyyyMMdd ->ddMMyy
	public static String convertYYYYMMDD_DDMMYY(String inputDate) {
		Date outDate = null;
		if (inputDate == null || inputDate.length() <= 0)
			return "";
		// set the format to use as a constructor argument
		SimpleDateFormat sdfEbank = new SimpleDateFormat("yyyyMMdd");
		SimpleDateFormat sdfCoreBank = new SimpleDateFormat("ddMMyy");

		if (inputDate.trim().length() != sdfEbank.toPattern().length())
			return "";
		sdfEbank.setLenient(false);
		try {
			// parse the inDate parameter
			outDate = sdfEbank.parse(inputDate.trim());
			return sdfCoreBank.format(outDate);
		} catch (ParseException pe) {
			return "";
		}
	}

	/**
	 * 
	 * @param listOfField
	 * @return
	 */
	public static String convertArrayToString(String[] listOfField) {
		StringBuffer buffSendMessage = new StringBuffer();
		if (listOfField != null) {
			for (int i = 1; i < listOfField.length; i++) {
				buffSendMessage.append((listOfField[i] == null ? "" : listOfField[i]) + "|");
			}
		} else {
			return "";
		}
		return buffSendMessage.toString();
	}

	/**
	 * Append from the left string n character
	 * 
	 * @version 1.0
	 */
	public static String padLeft(String s, int minLen, char ch) {
		int numPadChars = minLen - s.length();
		if (numPadChars <= 0)
			return s; // nothing to pad
		StringBuffer sb = new StringBuffer(minLen);
		for (int i = 0; i < numPadChars; i++)
			sb.append(ch);
		sb.append(s);
		return sb.toString();
	}

	/**
	 * Append from the Right string n character
	 * 
	 * @version 1.0
	 * @author Vu Trung Kien
	 */
	public static String padRight(String s, int minLen, char ch) {
		int numPadChars = minLen - s.length();
		if (numPadChars <= 0)
			return s; // nothing to pad
		StringBuffer sb = new StringBuffer(minLen);
		sb.append(s);
		for (int i = 0; i < numPadChars; i++)
			sb.append(ch);
		return sb.toString();
	}

	/**
	 * 
	 * @param orgStr
	 * @param specLength
	 * @return
	 */
	public static String subStringBySpecLength(String orgStr, int specLength) {
		int numOfChar = orgStr.length() - specLength;
		if (numOfChar < 0)
			return orgStr;
		else
			return orgStr.substring(0, orgStr.length() - specLength);
	}

	/**
	 * 
	 * @param str
	 * @return
	 */
	public static String stringToHexId(String str) {
		byte[] myId = str.getBytes();
		String hexAscii = "";
		for (int i = 0; i < myId.length; i++) {
			char b = (char) (myId[i] & 0xFF);
			if (b < 0x10) {
				hexAscii = hexAscii + "0";
			}
			hexAscii = hexAscii + (String) (Integer.toHexString(b)).toUpperCase();
		}
		return hexAscii;
	}

	/**
	 * 
	 * @param myId
	 * @return
	 */
	public static String dumpHexId(byte[] myId) {
		String retValue = "X'";
		for (int i = 0; i < myId.length; i++) {
			char b = (char) (myId[i] & 0xFF);
			if (b < 0x10) {
				retValue += "0";
			}
			retValue += (String) (Integer.toHexString(b)).toUpperCase();
		}
		retValue += "'";
		return retValue;
	}

	public static String correctStringValue(String inputValue) {
		if (inputValue == null || inputValue.length() == 0)
			return "";
		return String.valueOf(inputValue).trim();
	}

	public static void main(String args[]) {
		// String theNumber = convertEB2CBNumeric("340.02");
		// System.out.println(theNumber);
		System.out.println(isValidNumeric("123000tt0"));
	}

}
